package cn.ac.cintcm.dao.hibernate;

import java.util.List;

import javax.persistence.Table;

import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.jdbc.core.simple.SimpleJdbcTemplate;
import org.springframework.orm.hibernate3.SessionFactoryUtils;

import cn.ac.cintcm.dao.FeedbackDao;
import cn.ac.cintcm.model.FeedBack;

public  class FeedbackDaoGeneric extends GenericDaoHibernate<FeedBack, Long> implements FeedbackDao{
	
	public FeedbackDaoGeneric() {
		super(FeedBack.class);
	}

	public int getEffectiveCount(String user) {
        SimpleJdbcTemplate jdbcTemplate =
            new SimpleJdbcTemplate(SessionFactoryUtils.getDataSource(getSessionFactory()));
	    Table fbtable = AnnotationUtils.findAnnotation(FeedBack.class, Table.class);
	    StringBuilder sql = new StringBuilder();
	    sql.append("select count(*) from ");
	    sql.append(fbtable.name());
	    sql.append(" where author=0 and checked=0 and good=1 and doctor=");
    	sql.append(user);
    	log.debug(sql.toString());
	    return jdbcTemplate.queryForInt(sql.toString());

	}

	public int getProblematicCount(String user) {
        SimpleJdbcTemplate jdbcTemplate =
            new SimpleJdbcTemplate(SessionFactoryUtils.getDataSource(getSessionFactory()));
	    Table fbtable = AnnotationUtils.findAnnotation(FeedBack.class, Table.class);
	    StringBuilder sql = new StringBuilder();
	    sql.append("select count(*) from ");
	    sql.append(fbtable.name());
	    sql.append(" where author=0 and checked=0 and good=0 and doctor=");
    	sql.append(user);
    	log.debug(sql.toString());
	    return jdbcTemplate.queryForInt(sql.toString());
	}

	public int getUnreadCount(String user) {
        SimpleJdbcTemplate jdbcTemplate =
            new SimpleJdbcTemplate(SessionFactoryUtils.getDataSource(getSessionFactory()));
	    Table fbtable = AnnotationUtils.findAnnotation(FeedBack.class, Table.class);
	    StringBuilder sql = new StringBuilder();
	    sql.append("select count(*) from ");
	    sql.append(fbtable.name());
	    sql.append(" where author=0 and checked=0 and doctor=");

    	sql.append(user);
    	log.debug(sql.toString());
	    return jdbcTemplate.queryForInt(sql.toString());

	}

	@SuppressWarnings("unchecked")
	public List<FeedBack> getUnread(String user) {
		StringBuilder sql = new StringBuilder();
		sql.append("from FeedBack where author=0 and checked=0 and doctor.username='");
    	sql.append(user);
    	sql.append("'");
    	log.debug(sql.toString());
	    return  getHibernateTemplate().find(sql.toString());
	}

	@SuppressWarnings("unchecked")
	public List<FeedBack> getByTherapy(Long id) {
		String hsql = "from FeedBack where therapy=" + id;
	    return  getHibernateTemplate().find(hsql);
	}
	
	public List<FeedBack> getAll(String username) {
		String hsql = "from FeedBack where doctor.username='" + username + "'";
	    return  getHibernateTemplate().find(hsql);
	}
	
	public void clearUnread() {
        SimpleJdbcTemplate jdbcTemplate =
            new SimpleJdbcTemplate(SessionFactoryUtils.getDataSource(getSessionFactory()));
	    Table fbtable = AnnotationUtils.findAnnotation(FeedBack.class, Table.class);
	    StringBuilder sql = new StringBuilder();
	    sql.append("update ");
	    sql.append(fbtable.name());
	    sql.append(" set checked=? where checked=0");
	    jdbcTemplate.update(sql.toString(), 1);
	}
}
